﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DTS
{
    class AlgorithmController
    {
        private List<Algorithm> algorytmy;
        private AlgorithmRSI refRSI;
        public AlgorithmController()
        {
            algorytmy = new List<Algorithm>();
            algorytmy.Add(new AlgorithmSMA(100));
            algorytmy.Add(new AlgorithmSMA(50));
            algorytmy.Add(new AlgorithmSMA(25));
            algorytmy.Add(new AlgorithmSMA(200));
            algorytmy.Add(new AlgorithmEMA(15));
            algorytmy.Add(new AlgorithmEMA(20));
            algorytmy.Add(new AlgorithmEMA(30));
            refRSI = new AlgorithmRSI(15);
            algorytmy.Add(refRSI);           
            algorytmy.Add(new AlgorithmMINMAX(20));
            algorytmy.Add(new AlgorithmMINMAX(30));
            algorytmy.Add(new AlgorithmMINMAX(50));
            algorytmy.Add(new AlgorithmMACD());
        }
        public int getDecision(String algName, HistoriaSpolki hs, DateTime actualDay)
        {
            var algRef = algorytmy.Find(alg => alg.getName().Equals(algName));
            if (algRef == null) return -2;
            return algRef.getDecision(hs,actualDay);
        }
        public int getDecision(String algName, HistoriaSpolki hs, Double cena,DateTime actualDay)
        {
            var algRef = algorytmy.Find(alg => alg.getName().Equals(algName));
            if (algRef == null) return -2;
            return algRef.getDecision(hs,cena,actualDay);
        }
        public int getDecisionAll(HistoriaSpolki hs, Double cena, DateTime actualDay)
        {
            return 0;
        }
        public int getDecisionAll(HistoriaSpolki hs, DateTime actualDay)
        {
            int liczLong = 0;
            int liczShort = 0;
            foreach (var algorytm in algorytmy)
            {
                int decyzja = algorytm.getDecision(hs, actualDay);
                //Console.WriteLine(algorytm.getName() + "  " + decyzja);
                if (decyzja == 1) liczLong++;
                if (decyzja == -1) liczShort++;
            }
            if (liczLong != 0 && liczShort!=0) return 0;
            return liczLong-liczShort;
        }
        public void setProgEMA(Double val)
        {
            for (int i = 0; i < algorytmy.Count;i++)
            {
                var tmp = algorytmy[i];
                if (tmp.getName().StartsWith("EMA"))
                {
                    //Console.WriteLine(tmp.getName());
                    //Console.WriteLine(((AlgorithmEMA)algorytmy[i]).factor);
                    ((AlgorithmEMA)algorytmy[i]).setFactor(val);
                    //Console.WriteLine(((AlgorithmEMA)algorytmy[i]).factor);
                }
            }
        }
        public void setProgSMA(Double val)
        {
            for (int i = 0; i < algorytmy.Count; i++)
            {
                var tmp = algorytmy[i];
                if (tmp.getName().StartsWith("SMA"))
                {
                    //Console.WriteLine(tmp.getName());
                    //Console.WriteLine(((AlgorithmEMA)algorytmy[i]).factor);
                    ((AlgorithmSMA)algorytmy[i]).setFactor(val);
                    //Console.WriteLine(((AlgorithmEMA)algorytmy[i]).factor);
                }
            }
        }

    }
}
